#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File    : redis_obj.py
# @Time    : 2021/10/4 22:30
# @Author  : Karl·Marx
import redis


class RedisObj:

    def __init__(self, host='192.168.10.6', port=6379):
        self.pool = redis.ConnectionPool(host=host, port=port, decode_responses=True)
        self.redis_obj = redis.StrictRedis(connection_pool=self.pool, socket_timeout=3000)

    def redis_set(self, key, value, ex=3600):
        return self.redis_obj.set(key, value, ex=ex)

    def redis_get(self, key):
        return self.redis_obj.get(key)

    def redis_keys(self):
        return self.redis_obj.keys()

    def redis_del(self, *keys):
        return self.redis_obj.delete(*keys)

    def close(self):
        self.redis_obj.close()

    def redis_get_token(self, token_key_):
        if self.redis_obj.exists(token_key_) > 0:
            token_ = self.redis_obj.get(token_key_)
            if token_ is not None:
                return token_
            else:
                '''token已过期'''
                return None
        else:
            '''没有此key'''
            return None


if __name__ == '__main__':
    env = 'test'
    role = 'worker'
    user = 'wwc3d26752b91b70ac'
    token_key = f'{env}_{role}_{user}'
    redis = RedisObj()
    token = redis.redis_get_token(token_key)
    if token is not None:
        '''为接口base类设置token'''
        ...
    else:
        '''调登录接口获取token'''
        access_token = 'asdasd123131sdfds'
        '''为接口base类设置token'''
        ...
        '''为redis设置token'''
        redis.redis_obj.set(token_key, access_token, ex=3600)
